package com.zjw.dao.impl;

import com.zjw.dao.BaseDao;
import com.zjw.dao.SubjectDao;
import com.zjw.entity.Subject;
import com.zjw.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: studentCourseSelectionManagement
 * @package: com.zjw.dao.impl
 * @className: SubjectDaoImpl
 * @author: 翟
 * @date: 2025/4/7 0:13
 */

public class SubjectDaoImpl extends BaseDao implements SubjectDao {
    public SubjectDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public List<Subject> selectSubjectByGradeId(int gradeId) throws SQLException {
        String sql = "SELECT `SubjectNo`,`SubjectName`,`ClassHour` FROM `subject`\n" +
                "WHERE `GradeID` = ?";
        ResultSet resultSet = null;
        List<Subject> list = null;
        try {
            resultSet = executeQuery(sql , gradeId);
            list = new ArrayList<>();
            while (resultSet.next()){
                Subject subject = new Subject();
                subject.setSubjectNo(resultSet.getInt("SubjectNo"));
                subject.setSubjectName(resultSet.getString("SubjectName"));
                subject.setClassHour(resultSet.getInt("ClassHour"));
                list.add(subject);
            }

        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
        return list;
    }
}
